/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sochinchou.res.views;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.sochinchou.res.R;

public class MyCustomView extends View {
	private Paint mTextPaint;
	private String mText;
	private int mAscent;

	public MyCustomView(Context context) {
		super(context);
		initLabelView();
	}

	public MyCustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLabelView();

		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView);

		CharSequence s = typedArray.getString(R.styleable.MyCustomView_customText);

		if (s != null) {
			setText(s.toString());
		}

		setTextColor(typedArray.getColor(R.styleable.MyCustomView_customTextColor, 0xFF000000));

		int textSize = typedArray.getDimensionPixelOffset(R.styleable.MyCustomView_customTextSize, 0);
		if (textSize > 0) {
			setTextSize(textSize);
		}

		typedArray.recycle();
	}

	private final void initLabelView() {
		mTextPaint = new Paint();
		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
		mTextPaint.setColor(0xFF000000);
		setPadding(3, 3, 3, 3);
	}


	public void setText(String text) {
		mText = text;
		requestLayout();
		invalidate();
	}


	public void setTextSize(int size) {
		mTextPaint.setTextSize(size);
		requestLayout();
		invalidate();
	}


	public void setTextColor(int color) {
		mTextPaint.setColor(color);
		invalidate();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
	}


	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
			
		} else {

			result = (int) mTextPaint.measureText(mText) + getPaddingLeft() + getPaddingRight();
			
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}

		return result;
	}


	private int measureHeight(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		mAscent = (int) mTextPaint.ascent();
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
			
		} else {
			// Measure the text (beware: ascent is a negative number)
			result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop() + getPaddingBottom();
			if (specMode == MeasureSpec.AT_MOST) {

				result = Math.min(result, specSize);
			}
		}
		return result;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
	}
}
